<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Report_penerimaan_cabang extends Authenticated_Controller{
	function __construct() {
		parent::__construct();
		$this->load->model('pencabang_model','cabang');
	}
	
	function index(){
		$this->list_result();
	}
	
	function list_result($tgl_awal='',$tgl_akhir=''){
		$data['filter_cabang']=$this->cabang->get_all_cabang();
		
		if($tgl_awal==''){
			$data['tgl_awal']=date('m/d/Y');
		}else{
			$data['tgl_awal']=$this->date_func->change_format_date($tgl_awal,'d-m-Y','m/d/Y');
		}
		if($tgl_akhir==''){
			$data['tgl_akhir']=date('m/d/Y');
		}else{
			$data['tgl_akhir']=$this->date_func->change_format_date($tgl_akhir,'d-m-Y','m/d/Y');
		}

		$data['list_result'] = $this->cabang->get_data_table($tgl_awal,$tgl_akhir);
		$data['biaya_langsung'] = $this->cabang->get_biaya_langsung($tgl_awal,$tgl_akhir);
		$data['biaya_opawal'] = $this->cabang->get_biaya_opawal($tgl_awal,$tgl_akhir);

		$this->load->view('template/header_view');
		$this->load->view('pencabang/list_result',$data);
		$this->load->view('template/footer_view');
	}
        
        function grafik_harian_omset_cabang($cabang='',$bulan='',$tahun=''){
		$data['bulan'] = $this->cabang->get_month();
                $data['tahun'] = $this->cabang->get_year();
                $data['filter_cabang']=$this->cabang->get_all_cabang_tanpa_semua();
                if($bulan==''){
			$bulan=(date('m')-1).'';
		}
                if($tahun==''){
			$tahun=date('Y').'';
		}
                $data['current']['bulan'] = $bulan;
                $data['current']['tahun'] = $tahun;
                $data['current']['cabang'] = $cabang;
                $data['jumlah_hari'] = cal_days_in_month(CAL_GREGORIAN, $bulan+1, $tahun);
                $data['list_tujuan']=$this->cabang->get_grafik_hari_cabang_tujuan($cabang,$bulan+1,$tahun);
                $data['list_asal']=$this->cabang->get_grafik_hari_cabang_asal($cabang,$bulan+1,$tahun);
                $j=0;
                for($i=1;$i<=$data['jumlah_hari'];$i++)
                {
                    if(!isset($data['list_tujuan'][$j]))
                    {
                        $data['grafik'][$i-1] = '';
                        continue;
                    }
                    if($i==$data['list_tujuan'][$j]['tanggal'])
                    {
                        $data['grafik'][$i-1] = $data['list_tujuan'][$j];
                        $j++;
                    }
                    else {
                        $data['grafik'][$i-1] = '';
                    }
                }
                $j=0;
                for($i=1;$i<=$data['jumlah_hari'];$i++)
                {
                    if(!isset($data['list_asal'][$j]))
                    {
                        $data['grafik_asal'][$i-1] = '';
                        continue;
                    }
                    if($i==$data['list_asal'][$j]['tanggal'])
                    {
                        $data['grafik_asal'][$i-1] = $data['list_asal'][$j];
                        $j++;
                    }
                    else {
                        $data['grafik_asal'][$i-1] = '';
                    }
                }
		$this->load->view('template/header_view');
		$this->load->view('laporan/grafik_harian_omset_cabang',$data);
		$this->load->view('template/footer_view');
	}
        
        function grafik_bulanan_omset_cabang($cabang='',$tahun=''){
		$data['bulan'] = $this->cabang->get_month();
                $data['tahun'] = $this->cabang->get_year();
                $data['filter_cabang']=$this->cabang->get_all_cabang_tanpa_semua();
                if($tahun==''){
			$tahun=date('Y').'';
		}
                $data['current']['tahun'] = $tahun;
                $data['current']['cabang'] = $cabang;
                $data['list_tujuan']=$this->cabang->get_grafik_bulan_cabang_tujuan($cabang,$tahun);
                $data['list_asal']=$this->cabang->get_grafik_bulan_cabang_asal($cabang,$tahun);
                $j=0;
                for($i=1;$i<=count($data['bulan']);$i++)
                {
                    if(!isset($data['list_tujuan'][$j]))
                    {
                        $data['grafik'][$i-1] = '';
                        continue;
                    }
                    if($i==$data['list_tujuan'][$j]['tanggal'])
                    {
                        $data['grafik'][$i-1] = $data['list_tujuan'][$j];
                        $j++;
                    }
                    else {
                        $data['grafik'][$i-1] = '';
                    }
                }
                $j=0;
                for($i=1;$i<=count($data['bulan']);$i++)
                {
                    if(!isset($data['list_asal'][$j]))
                    {
                        $data['grafik_asal'][$i-1] = '';
                        continue;
                    }
                    if($i==$data['list_asal'][$j]['tanggal'])
                    {
                        $data['grafik_asal'][$i-1] = $data['list_asal'][$j];
                        $j++;
                    }
                    else {
                        $data['grafik_asal'][$i-1] = '';
                    }
                }
		$this->load->view('template/header_view');
		$this->load->view('laporan/grafik_bulanan_omset_cabang',$data);
		$this->load->view('template/footer_view');
	}
        
        function grafik_harian_produk_cabang($cabang='',$bulan='',$tahun=''){
		$data['bulan'] = $this->cabang->get_month();
                $data['tahun'] = $this->cabang->get_year();
                $data['filter_cabang']=$this->cabang->get_all_cabang_tanpa_semua();
                if($bulan==''){
			$bulan=(date('m')-1).'';
		}
                if($tahun==''){
			$tahun=date('Y').'';
		}
                $data['current']['bulan'] = $bulan;
                $data['current']['tahun'] = $tahun;
                $data['current']['cabang'] = $cabang;
                $data['jumlah_hari'] = cal_days_in_month(CAL_GREGORIAN, $bulan+1, $tahun);
                $data['list_tujuan']=$this->cabang->get_grafik_hari_produk_cabang($cabang,$bulan+1,$tahun);
                $j=0;
                for($i=1;$i<=$data['jumlah_hari'];$i++)
                {
                    if(!isset($data['list_tujuan'][$j]))
                    {
                        $data['grafik'][$i-1] = '';
                        continue;
                    }
                    if($i==$data['list_tujuan'][$j]['tanggal'])
                    {
                        $data['grafik'][$i-1] = $data['list_tujuan'][$j];
                        $j++;
                    }
                    else {
                        $data['grafik'][$i-1] = '';
                    }
                }
		$this->load->view('template/header_view');
		$this->load->view('laporan/grafik_harian_produk_cabang',$data);
		$this->load->view('template/footer_view');
	}
        
        function grafik_bulanan_produk_cabang($cabang='',$tahun=''){
		$data['bulan'] = $this->cabang->get_month();
                $data['tahun'] = $this->cabang->get_year();
                $data['filter_cabang']=$this->cabang->get_all_cabang_tanpa_semua();
                if($tahun==''){
			$tahun=date('Y').'';
		}
                $data['current']['tahun'] = $tahun;
                $data['current']['cabang'] = $cabang;
                $data['list_tujuan']=$this->cabang->get_grafik_bulan_produk_cabang($cabang,$tahun);
                $j=0;
                for($i=1;$i<=count($data['bulan']);$i++)
                {
                    if(!isset($data['list_tujuan'][$j]))
                    {
                        $data['grafik'][$i-1] = '';
                        continue;
                    }
                    if($i==$data['list_tujuan'][$j]['tanggal'])
                    {
                        $data['grafik'][$i-1] = $data['list_tujuan'][$j];
                        $j++;
                    }
                    else {
                        $data['grafik'][$i-1] = '';
                    }
                }
                
		$this->load->view('template/header_view');
		$this->load->view('laporan/grafik_bulanan_produk_cabang',$data);
		$this->load->view('template/footer_view');
	}
        
	
	function cetak_pdf($tgl_awal,$tgl_akhir,$cabang){
		$data['filter_cabang']=$this->cabang->get_all_cabang();
		$data['cabang']=$cabang;
		$data['tgl_awal']=$tgl_awal;
		$data['tgl_akhir']=$tgl_akhir;
		if($cabang!='SEMUA'){
			$data['list_result'] = $this->cabang->get_data_table_cabang($tgl_awal,$tgl_akhir,$cabang);
			$data['biaya_langsung'] = $this->cabang->get_biaya_langsung_cabang($tgl_awal,$tgl_akhir,$cabang);
			$data['biaya_opawal'] = $this->cabang->get_biaya_opawal_cabang($tgl_awal,$tgl_akhir,$cabang);
		}else{
			$data['list_result'] = $this->cabang->get_data_table($tgl_awal,$tgl_akhir);
			$data['biaya_langsung'] = $this->cabang->get_biaya_langsung($tgl_awal,$tgl_akhir);
			$data['biaya_opawal'] = $this->cabang->get_biaya_opawal($tgl_awal,$tgl_akhir);
		}
		$html = $this->load->view('pencabang/html_cetak',$data,TRUE);
		$this->load->library('pdf');
		$this->pdf->to_pdf_landscape($html,'Report Penerimaan Cabang.pdf');
	}
	
	function cetak_excel($tgl_awal,$tgl_akhir,$cabang){
		$this->load->library('excel');
		$data['filter_cabang']=$this->cabang->get_all_cabang();
		$data['cabang']=$cabang;
		$data['tgl_awal']=$tgl_awal;
		$data['tgl_akhir']=$tgl_akhir;
		if($cabang!='SEMUA'){
			$data['list_result'] = $this->cabang->get_data_table_cabang($tgl_awal,$tgl_akhir,$cabang);
			$data['biaya_langsung'] = $this->cabang->get_biaya_langsung_cabang($tgl_awal,$tgl_akhir,$cabang);
			$data['biaya_opawal'] = $this->cabang->get_biaya_opawal_cabang($tgl_awal,$tgl_akhir,$cabang);
		}else{
			$data['list_result'] = $this->cabang->get_data_table($tgl_awal,$tgl_akhir);
			$data['biaya_langsung'] = $this->cabang->get_biaya_langsung($tgl_awal,$tgl_akhir);
			$data['biaya_opawal'] = $this->cabang->get_biaya_opawal($tgl_awal,$tgl_akhir);
		}
		$objPHPExcel = new PHPExcel();
		$objPHPExcel->getProperties()->setTitle('title')->setDescription('description');

		$objPHPExcel->setActiveSheetIndex(0);
		//Set Keterangan
		$objPHPExcel->getActiveSheet()->setCellValue('A1', 'Laporan Penerimaan Cabang');
		$objPHPExcel->getActiveSheet()->setCellValue('A2', 'Periode');
		$objPHPExcel->getActiveSheet()->setCellValue('B2', $data['tgl_awal'].'-'.$data['tgl_akhir']);
		$objPHPExcel->getActiveSheet()->setCellValue('A3', 'Cabang');
		$objPHPExcel->getActiveSheet()->setCellValue('B3', $cabang);
		//Set header
		$objPHPExcel->getActiveSheet()->setCellValue('A4', 'No');
		$objPHPExcel->getActiveSheet()->setCellValue('B4', 'Nama');
		$objPHPExcel->getActiveSheet()->setCellValue('C4', 'Kode Cabang');
		$objPHPExcel->getActiveSheet()->setCellValue('D4', 'Alamat');
		$objPHPExcel->getActiveSheet()->setCellValue('E4', 'Telp');
		$objPHPExcel->getActiveSheet()->setCellValue('F4', 'Total Tiket');
		$objPHPExcel->getActiveSheet()->setCellValue('G4', 'Total Tiket Void');
		$objPHPExcel->getActiveSheet()->setCellValue('H4', 'Total Omset');
		$objPHPExcel->getActiveSheet()->setCellValue('I4', 'Total Discount');
		$objPHPExcel->getActiveSheet()->setCellValue('J4', 'Total Fee Agen');
		$objPHPExcel->getActiveSheet()->setCellValue('K4', 'Total Void');
		$objPHPExcel->getActiveSheet()->setCellValue('L4', 'Total Biaya Langsung');
		$objPHPExcel->getActiveSheet()->setCellValue('M4', 'Total Setor');
		$objPHPExcel->getActiveSheet()->setCellValue('N4', 'Total Biaya OP Awal');
		$objPHPExcel->getActiveSheet()->setCellValue('O4', 'Total Laba Kotor');
		// Save it as an excel 2003 file
		$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
		
		$row=5;
		
		for($i=0;$i<count($data['list_result']);$i++){
			$objPHPExcel->getActiveSheet()->setCellValue('A'.$row, ($i+1));
			$objPHPExcel->getActiveSheet()->setCellValue('B'.$row, $data['list_result'][$i]['cabang_keberangkatan']);
			$objPHPExcel->getActiveSheet()->setCellValue('C'.$row, $data['filter_cabang']['kode_cabang'][$data['list_result'][$i]['cabang_keberangkatan']]);
			$objPHPExcel->getActiveSheet()->setCellValue('D'.$row, $data['filter_cabang']['alamat_cabang'][$data['list_result'][$i]['cabang_keberangkatan']]);
			$objPHPExcel->getActiveSheet()->setCellValue('E'.$row, $data['filter_cabang']['telp_cabang'][$data['list_result'][$i]['cabang_keberangkatan']]);
			$objPHPExcel->getActiveSheet()->setCellValue('F'.$row, $data['list_result'][$i]['total_penumpang']);
			$objPHPExcel->getActiveSheet()->setCellValue('G'.$row, $data['list_result'][$i]['tiket_void']);
			$objPHPExcel->getActiveSheet()->setCellValue('H'.$row, 'Rp. '.number_format($data['list_result'][$i]['total_omset'],0,',','.'));
			$objPHPExcel->getActiveSheet()->setCellValue('I'.$row, 'Rp. '.number_format($data['list_result'][$i]['total_discount'],0,',','.'));
			$objPHPExcel->getActiveSheet()->setCellValue('J'.$row, 'Rp. '.number_format($data['list_result'][$i]['fee_agen'],0,',','.'));
			$objPHPExcel->getActiveSheet()->setCellValue('K'.$row, 'Rp. '.number_format($data['list_result'][$i]['total_void'],0,',','.'));
			$objPHPExcel->getActiveSheet()->setCellValue('L'.$row, 'Rp. '.number_format($data['biaya_langsung'][$data['list_result'][$i]['cabang_keberangkatan']],0,',','.'));
			$objPHPExcel->getActiveSheet()->setCellValue('M'.$row, 'Rp. '.number_format($data['list_result'][$i]['total_setoran'],0,',','.'));
			$objPHPExcel->getActiveSheet()->setCellValue('N'.$row, 'Rp. '.number_format($data['biaya_opawal'][$data['list_result'][$i]['cabang_keberangkatan']],0,',','.'));
			$objPHPExcel->getActiveSheet()->setCellValue('O'.$row, 'Rp. '.number_format($data['list_result'][$i]['total_laba'],0,',','.'));
		
			$row++;
		}
		
		
		$objWriter->save('public/Report Penerimaan Cabang '.$tgl_awal.'-'.$tgl_akhir.'-'.$cabang.'.xls');
		redirect(PUBLIC_FOLDER.'Report Penerimaan Cabang '.$tgl_awal.'-'.$tgl_akhir.'-'.$cabang.'.xls');
	}
}